package android_rteditor.customsortricheditor.editor;

import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import android_rteditor.customsortricheditor.R;

/**
 * 带有checkbox的editText
 * Created by chenxunlin01 on 2017/8/16.
 */

public class CheckboxView extends LinearLayout implements ITextEditorItem {

    private OnEditorFocusChangeListener editorFocusChangeListener;
    private EditText editText;
    private RichEditorController controller;

    public CheckboxView(Context context, RichEditorController controller, OnEditorFocusChangeListener listener) {
        super(context);
        editorFocusChangeListener = listener;
        this.controller = controller;
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.checkbox_view, this);
        editText = (EditText) findViewById(R.id.editorText);
        editText.setPadding(0, 0, 0, 0);
        editText.setLineSpacing(8, 1);
        setEditorBackgroundColor(controller.getEditTextBackgroundColor());
        setEditorTextColor(controller.getEditTextColor());
        setEditorTextSize(controller.getEditTextSize());
        editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                editorFocusChangeListener.onFocusChange(CheckboxView.this, hasFocus);
            }
        });
        editText.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                    onBackspacePress();
                } else if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    onEnter();
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public int getType() {
        return EditorItemFactory.EDITOR_ITEM_CHECKBOX;
    }

    private void onEnter() {
        int index = controller.indexOfItem(CheckboxView.this);
        CheckboxView checkboxView = controller.getEditorDomUtils().createEditorCheckboxItem();
        controller.addEditorItem(checkboxView, index + 1);
        checkboxView.focus(false);
    }

    private void onBackspacePress() {
        int startSelection = editText.getSelectionStart();
        // 只有在光标已经顶到文本输入框的最前方，两个View合并
        if (startSelection == 0) {
            RichEditText richEditText = controller.getEditorDomUtils().createEditorTextItem();
            controller.getEditorDomUtils().checkboxToText(CheckboxView.this, richEditText);
        }
    }

    // 根据index获取焦点
    @Override
    public void focusByStartEnd(int start, int end) {
        requestEditorFocus(editText);
        String text = editText.getText().toString();
        if (start < 0 || start > text.length()) {
            start = 0;
        }
        if (end < 0 || end > text.length()) {
            end = text.length();
        }

        editText.setSelection(start, end);
    }

    /**
     * 获取光标位置
     *
     * @return
     */
    @Override
    public int[] getSelectionStartEnd() {
        int[] startEnd = new int[2];
        startEnd[0] = editText.getSelectionStart();
        startEnd[1] = editText.getSelectionEnd();
        return startEnd;
    }

    // 防止已经有焦点时调用requestFocus，不会触发事件
    @Override
    public void requestEditorFocus() {
        editText.requestFocus();
        editorFocusChangeListener.onFocusChange(this, true);
    }

    @Override
    public String getEditorText() {
        return editText.getText().toString();
    }

    @Override
    public void setEditorText(String text) {
        editText.setText(text);
    }

    @Override
    public void setEditorTextSize(float size) {
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
    }

    @Override
    public void setEditorTextColor(int colorRes) {
        editText.setTextColor(colorRes);
    }

    @Override
    public void setEditorBackgroundColor(int colorRes) {
        editText.setBackgroundColor(colorRes);
    }

    private void requestEditorFocus(EditText editText) {
        editText.requestFocus();
        editorFocusChangeListener.onFocusChange(this, true);
    }

    /**
     * 文本内容是否为空
     *
     * @return
     */
    @Override
    public boolean isEmptyText() {
        return TextUtils.isEmpty(editText.getText().toString());
    }

    @Override
    public void focus(boolean toEnd) {
        requestEditorFocus();
        String text = editText.getText().toString();
        if (toEnd) {
            focusByStartEnd(text.length(), text.length());
        } else {
            focusByStartEnd(0, 0);
        }
    }
}
